package com.infocollect.project.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.infocollect.common.utils.ServletUtils;
import com.infocollect.framework.security.LoginUser;
import com.infocollect.framework.security.service.TokenService;
import com.infocollect.project.api.entity.TappCommonNoEntity;
import com.infocollect.project.api.helper.CurrentHelp;
import com.infocollect.project.api.mapper.TappCommonNoMapper;
import com.infocollect.project.api.service.ITappCommonNoService;
import com.infocollect.project.api.vo.CurrentUserInfoVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service("tappCommonnoService")
public class TappCommonNoServiceImpl extends ServiceImpl<TappCommonNoMapper, TappCommonNoEntity> implements ITappCommonNoService {
    @Autowired
    private TokenService tokenService;
    /**
     * 通过ID查询单条数据
     *
     * @param fid 主键
     * @return 实例对象
     */
    @Override
    public TappCommonNoEntity queryById(Long fid) {
        return this.baseMapper.queryById(fid);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<TappCommonNoEntity> queryAllByLimit(int offset, int limit) {
        return this.baseMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param tappcommonno 实例对象
     * @return 实例对象
     */
    @Override
    public TappCommonNoEntity insert(TappCommonNoEntity tappcommonno) {
        this.baseMapper.insert(tappcommonno);
        return tappcommonno;
    }

    /**
     * 修改数据
     *
     * @param tappcommonno 实例对象
     * @return 实例对象
     */
    @Override
    public TappCommonNoEntity update(TappCommonNoEntity tappcommonno) {
        this.baseMapper.updateById(tappcommonno);
        return this.queryById(tappcommonno.getFid());
    }

    /**
     * 通过主键删除数据
     *
     * @param fid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long fid) {
        return this.baseMapper.deleteById(fid) > 0;
    }

    /**
     * 查询多条数据
     * @menu 根据实体对象是否有值 进行查询
     * @return 对象列表
     */
    @Override
    public List<TappCommonNoEntity> queryAll(TappCommonNoEntity entity){
        QueryWrapper<TappCommonNoEntity> queryWrapper=new QueryWrapper();
        queryWrapper.eq("fmoduleid",entity.getFmoduleid());
        queryWrapper.eq("fmodulename",entity.getFmodulename());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(new Date());
        queryWrapper.eq("date_format(fcreationdate,'%Y-%m-%d')",dateString);
        return baseMapper.selectList(queryWrapper);
    }
    @Override
    public String GetProcInstNumber(String bMoudleEN,String bModuleID) throws IOException {
        TappCommonNoEntity entity = new TappCommonNoEntity();
        if (StringUtils.isNotBlank(bMoudleEN) && StringUtils.isNotBlank(bModuleID))
        {
            entity.setFmodulename(bMoudleEN);
            entity.setFmoduleid(bModuleID);
        }
        else
        {
            //01 没有配置时默认为wmp模块生产的编号
            entity.setFmodulename("wmp");
            entity.setFmoduleid("M20110803110024");
        }
        entity.setFcreationdate(new Date());
//        CurrentUserInfoVO currentUserInfo = CurrentHelp.Ins.GetUser();
        LoginUser currentUserInfo = tokenService.getLoginUser(ServletUtils.getRequest());
        try
        {   //02 获取当天该模块最大流水号
            int procInstNo = 0;
            List<TappCommonNoEntity> tappCommonnoList =queryAll(entity);// db.TB_Common_NumberID.Where(x => x.FModuleName == entity.FModuleName && x.FCreationDate.Value.Day == DateTime.Now.Day && x.FCreationDate.Value.Year == DateTime.Now.Year && x.FCreationDate.Value.Month == DateTime.Now.Month).OrderByDescending(ee => ee.FOrderID).FirstOrDefault(); ///new Achievo.MMIP.CommonService.CommonNumberService().GetMaxOrderIDByMoudleName(entity.FModuleName);
            if (!tappCommonnoList.isEmpty())
            {
                entity=tappCommonnoList.get(0);
                procInstNo = entity.getForderid();
            }
            entity.setForderid(procInstNo + 1);
            String procInstNumber= new SimpleDateFormat("yyyyMMdd").format(new Date());;
            ///db.InserByMoudleName(entity.FID, entity.FModuleID, entity.FModuleName, CurrentUserID, entity.FCreationDate, null, CurrentUserID, entity.FCreationDate, objectParams);

            String order = "000" + entity.getForderid();
            if (order.length() > 4)
                order = order.substring(order.length() - 4, order.length());
            // 由于碰到没有预见性的人的意见，此处我们难受的去掉“-”
            //procInstNumber = entity.FModuleName.ToUpper() + procInstNumber + "-" + order;
            procInstNumber = entity.getFmodulename().toUpperCase() + procInstNumber + order;
            if(entity.getFid()!=null)
                baseMapper.updateById(entity);
            else
                baseMapper.insert(entity);
              return procInstNumber;

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}
